export function EnsureDocumentLoaded(): Promise<void> {
    if (document.readyState == "complete")
        return Promise.resolve();

    return new Promise(resolve => document.addEventListener("DOMContentLoaded", () => resolve()));
}

export function EnsureJS(url: string): Promise<void> {
    if (document.querySelector(`script[src='${url}']`) != null)
        return Promise.resolve();

    const s = document.createElement("script");
    s.src = url;
    s.type = 'text/javascript';
    document.head.appendChild(s);

    return new Promise(resolve => s.addEventListener("load", () => resolve()));
}

export function EnsureCSS(url: string): Promise<void> {
    if (document.querySelector(`link[href='${url}']`) != null)
        return Promise.resolve();

    const s = document.createElement("link");
    s.href = url;
    s.rel = 'stylesheet';
    document.head.appendChild(s);

    return new Promise(resolve => s.addEventListener("load", () => resolve()));
}